import React, {Component, Fragment} from 'react';
import {Table} from 'antd';
import "./UpperPartOfTheForm.css"

export default class CKssylqfxhzb extends Component {

    constructor(props) {
        super(props);
        this.state = {
            Time: [],//第一列的时间变化自动控制
            Data: [],//原始填写的数据
            passRate: [],//合格率
            average: [],//平均
        }
    }

    componentWillMount() {
        const allTime = [
            ['0:00', '1:00', '2:00', '3:00', '4:00', '5:00', '6:00', '7:00'],
            ['8:00', '9:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00'],
            ['16:00', '17:00', '18:00', '19:00', '20:00', '21:00', '22:00', '23:00']
        ];
        const standard = [];
        standard[8] = this.props.standard[0];
        standard[10] = this.props.standard[1];
        standard[11] = this.props.standard[2];
        this.setState({
            Time: [...allTime[this.props.timeChose]],
            Data: this.props.upperData,
            standard: standard,
        })

    }

    componentDidMount() {
        this.updataData_Initial(this.props.standard)
    }

    componentWillReceiveProps(nextProps) {
        const allTime = [
            ['0:00', '1:00', '2:00', '3:00', '4:00', '5:00', '6:00', '7:00'],
            ['8:00', '9:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00'],
            ['16:00', '17:00', '18:00', '19:00', '20:00', '21:00', '22:00', '23:00']
        ];
        const standard = [];
        standard[8] = this.props.standard[0];
        standard[10] = this.props.standard[1];
        standard[11] = this.props.standard[2];
        this.setState({
            Time: [...allTime[nextProps.timeChose]],
            Data: nextProps.upperData,
        });
        this.updataData_Initial(standard);
    }


    /***
     * 进行底部的合格率、平均数的计算更新以及是否正常的判断
     **/
    updataData_Initial(standard) {
        let temp1 = 0;//计算均值和合格率的临时数据
        let temp2 = 0;
        let temp3 = 0;
        let str = '';//计算合格比率的临时数据
        let inputCount0 = [], inputCount8 = [], inputCount16 = [];//各班次输入的总数
        let passCount0 = [], passCount8 = [], passCount16 = [];//各班次的合格数
        let passRate0 = [], passRate8 = [], passRate16 = [];//各班次的合格率
        let ratio0 = [], ratio8 = [], ratio16 = [];//各班次的合格比率
        //计算合格率部分
        for (let i = 0; i < 17; i++) {//i是列
            inputCount0[i] = 0;//0点班输入的总数
            passCount0[i] = 0;//0点班合格数
            inputCount8[i] = 0;//8点班输入的总数
            passCount8[i] = 0;//8点班合格数
            inputCount16[i] = 0;//16点班输入的总数
            passCount16[i] = 0;//16点班合格数
            for (let j = 0; j <= 7; j++) {//j是行
                if (!isNaN(this.state.Data[j]['shuju'][i]) && (this.state.Data[j]['shuju'][i] != null)) {
                    inputCount0[i]++;
                    if (this.state.Data[j]['shuju'][i] <= standard[i]) {
                        passCount0[i]++;
                    }
                }
                if (inputCount0[i] === 0) {
                    str = null;
                    temp1 = null;
                }
                else {
                    str = passCount0[i] + '\/' + inputCount0[i];
                    if (isNaN(temp1 = passCount0[i] / inputCount0[i])) {
                        temp1 = null;
                    }
                    else {
                        temp1 = Number(temp1 * 100).toFixed(1) + '%';
                    }
                }
                ratio0[i] = str;
                passRate0[i] = temp1;
            }
            for (let j = 8; j <= 15; j++) {//j是行
                if (!isNaN(this.state.Data[j]['shuju'][i]) && (this.state.Data[j]['shuju'][i] != null)) {
                    inputCount8[i]++;
                    if (this.state.Data[j]['shuju'][i] <= standard[i]) {
                        passCount8[i]++;
                    }
                }
                if (inputCount8[i] === 0) {
                    str = null;
                    temp1 = null;
                }
                else {
                    str = passCount8[i] + '\/' + inputCount8[i];
                    if (isNaN(temp1 = passCount8[i] / inputCount8[i])) {
                        temp1 = null;
                    }
                    else {
                        temp1 = Number(temp1 * 100).toFixed(1) + '%';
                    }
                }
                ratio8[i] = str;
                passRate8[i] = temp1;
            }
            for (let j = 16; j <= 23; j++) {//j是行
                if (!isNaN(this.state.Data[j]['shuju'][i]) && (this.state.Data[j]['shuju'][i] != null)) {
                    inputCount16[i]++;
                    if (this.state.Data[j]['shuju'][i] <= standard[i]) {
                        passCount16[i]++;
                    }
                }
                if (inputCount16[i] === 0) {
                    str = null;
                    temp1 = null;
                }
                else {
                    str = passCount16[i] + '\/' + inputCount16[i];
                    if (isNaN(temp1 = passCount16[i] / inputCount16[i])) {
                        temp1 = null;
                    }
                    else {
                        temp1 = Number(temp1 * 100).toFixed(1) + '%';
                    }
                }
                ratio16[i] = str;
                passRate16[i] = temp1;
            }
            let ratio = ratio0.concat(ratio8);
            ratio = ratio.concat(ratio16);
            let passRate = passRate0.concat(passRate8);
            passRate = passRate.concat(passRate16);
            this.setState({
                ratio: ratio,
                passRate: passRate,
            })
        }
        //计算均值的部分
        let average0 = [], average8 = [], average16 = [];//各班次的合格率
        for (let i = 0; i < 17; i++) {
            temp1 = 0;
            temp2 = 0;
            temp3 = 0;
            if (inputCount0[i] === 0) {
                temp1 = null;
            }
            else {
                for (let j = 0; j < 8; j++) {
                    if (!isNaN(this.state.Data[j]['shuju'][i])) {
                        temp1 += this.state.Data[j]['shuju'][i];
                    }
                }
                if (isNaN(temp1 = temp1 / inputCount0[i])) {
                    temp1 = 0;
                }
                temp1 = temp1.toFixed(3);
            }
            average0[i] = temp1;
            if (inputCount8[i] === 0) {
                temp2 = null;
            }
            else {
                for (let j = 8; j < 16; j++) {
                    if (!isNaN(this.state.Data[j]['shuju'][i])) {
                        temp2 += this.state.Data[j]['shuju'][i];
                    }
                }
                if (isNaN(temp2 = temp2 / inputCount8[i])) {
                    temp2 = 0;
                }
                temp2 = temp2.toFixed(3);
            }
            average8[i] = temp2;
            if (inputCount16[i] === 0) {
                temp3 = null;
            }
            else {
                for (let j = 16; j < 24; j++) {
                    if (!isNaN(this.state.Data[j]['shuju'][i])) {
                        temp3 += this.state.Data[j]['shuju'][i];
                    }
                }
                if (isNaN(temp3 = temp3 / inputCount16[i])) {
                    temp3 = 0;
                }
                temp3 = temp3.toFixed(3);
            }
            average16[i] = temp3;
        }
        let average = average0.concat(average8);
        average = average.concat(average16);
        this.setState({
            average: average
        });
        //是否正常的更新部分
        let newNormal = [];
        for (let i = 0; i < 24; i++) {
            if (!isNaN(this.state.Data[i]['shuju'][0]) && (this.state.Data[i]['shuju'][0] != null)) {
                if (this.state.Data[i]['shuju'][0] <= standard[0]) {
                    newNormal[i] = '正常';
                }
                else {
                    newNormal[i] = '异常';
                }
            }
            else {
                newNormal[i] = '';
            }
        }
        this.setState({
            isNormal: newNormal
        })
    }


    render() {
        // 表头
        const columns = [
            {
                title: '时间',
                key: 'time',
                dataIndex: 'time',
            },
            {
                title: 'SO3/%',
                dataIndex: 'SanYangHL',
                key: 'SanYangHL',
            },
            {
                title: 'SiO2/%',
                key: 'ErYangHG',
                dataIndex: 'ErYangHG',
            },
            {
                title: 'Ae2O3/%',
                key: 'Ae2O3',
                dataIndex: 'Ae2O3',
            },
            {
                title: 'Fe2O3/%',
                key: 'SanYangHET',
                dataIndex: 'SanYangHET',
            },
            {
                title: 'CaO/%',
                key: 'YangHuaG',
                dataIndex: 'YangHuaG',
            },
            {
                title: 'MgO/%',
                key: 'YangHuaM',
                dataIndex: 'YangHuaM',
            },
            {
                title: '合计',
                key: 'HiJi',
                dataIndex: 'HeJi',
            },
            {
                title: 'ICaO/%',
                key: 'ICaO',
                dataIndex: 'ICaO',
            },
            {
                title: 'KH/%',
                key: 'KH',
                dataIndex: 'KH',
            },
            {
                title: 'KH1/%',
                key: 'KH1',
                dataIndex: 'KH1',
            },
            {
                title: 'n/%',
                key: 'n',
                dataIndex: 'n',
            },
            {
                title: 'P/%',
                key: 'Lin',
                dataIndex: 'Lin',
            },
            {
                title: 'C3S/%',
                key: 'GuiSuanSG',
                dataIndex: 'GuiSuanSG',
            },
            {
                title: 'C2S/%',
                key: 'GuiSuanEG',
                dataIndex: 'GuiSuanEG',
            },
            {
                title: 'C3A/%',
                key: 'LuSuanSG',
                dataIndex: 'LuSuanSG',
            },
            {
                title: 'C4AF/%',
                key: 'TieLuSSG',
                dataIndex: 'TieLuSSG',
            },
            {
                title: 'CaCO3/%',
                key: 'TanSuanG',
                dataIndex: 'TanSuanG',
            },
            {
                title: '人员',
                key: 'person',
                dataIndex: 'person',
            },
        ];
        const data = [];
        //中间八行的数据输入
        const Data = this.state.Data;
        for (let i = 0; i < 8; i++) {
            let xiaoshi = i + this.props.timeChose * 8;
            const value = Data[xiaoshi]['shuju'];
            // console.log(value)
            data.push(
                {
                    time: this.state.Time[i],
                    SanYangHL: isNaN(value[0]) ? null : value[0],
                    ErYangHG: isNaN(value[1]) ? null : value[1],
                    Ae2O3: isNaN(value[2]) ? null : value[2],
                    SanYangHET: isNaN(value[3]) ? null : value[3],
                    YangHuaG: isNaN(value[4]) ? null : value[4],
                    YangHuaM: isNaN(value[5]) ? null : value[5],
                    HeJi: isNaN(value[6]) ? null : value[6],
                    ICaO: isNaN(value[7]) ? null : value[7],
                    KH: isNaN(value[8]) ? null : value[8],
                    KH1: isNaN(value[9]) ? null : value[9],
                    n: isNaN(value[10]) ? null : value[10],
                    Lin: isNaN(value[11]) ? null : value[11],
                    GuiSuanSG: isNaN(value[12]) ? null : value[12],
                    GuiSuanEG: isNaN(value[13]) ? null : value[13],
                    LuSuanSG: isNaN(value[14]) ? null : value[14],
                    TieLuSSG: isNaN(value[15]) ? null : value[15],
                    TanSuanG: isNaN(value[16]) ? null : value[16],
                    person: Data[xiaoshi]['name'],
                }
            )
        }
        //数据的自动处理显示部分
        const xiaoshi = this.props.timeChose * 17;
        data.push(
            {
                time: '平均',
                SanYangHL: this.state.average[0 + xiaoshi],
                ErYangHG: this.state.average[1 + xiaoshi],
                Ae2O3: this.state.average[2 + xiaoshi],
                SanYangHET: this.state.average[3 + xiaoshi],
                YangHuaG: this.state.average[4 + xiaoshi],
                YangHuaM: this.state.average[5 + xiaoshi],
                HeJi: this.state.average[6 + xiaoshi],
                ICaO: this.state.average[7 + xiaoshi],
                KH: this.state.average[8 + xiaoshi],
                KH1: this.state.average[9 + xiaoshi],
                n: this.state.average[10 + xiaoshi],
                P: this.state.average[11 + xiaoshi],
                GuiSuanSG: this.state.average[12 + xiaoshi],
                GuiSuanEG: this.state.average[13 + xiaoshi],
                LuSuanSG: this.state.average[14 + xiaoshi],
                TieLuSSG: this.state.average[15 + xiaoshi],
                TanSuanG: this.state.average[16 + xiaoshi],

            },
            {
                time: '合格率',
                SanYangHL: '--',
                ErYangHG: '--',
                Ae2O3: '--',
                SanYangHET: '--',
                YangHuaG: '--',
                YangHuaM: '--',
                HeJi: '--',
                ICaO: '--',
                KH: this.state.passRate[8 + xiaoshi],
                KH1: '--',
                n: this.state.passRate[10 + xiaoshi],
                P: this.state.passRate[11 + xiaoshi],
                GuiSuanSG: '--',
                GuiSuanEG: '--',
                LuSuanSG: '--',
                TieLuSSG: '--',
                TanSuanG: '--',
            }
        );


        return (
            <Fragment>
                <div style={this.props.style}>
                    <h1 align="center">{this.props.title}</h1>
                    <span style={{float: "center", font: '20px', paddingLeft: 20}}>日期：{this.props.riqi}</span>
                    <span
                        style={{float: "center", font: '20px', paddingLeft: 20}}>班次：{this.props.timeChose * 8}点班</span>
                    <div className="upper">
                        {/*表格填写*/}
                        <Table style={this.props.style} columns={columns} bordered dataSource={data}
                               pagination={false}/>
                    </div>
                </div>

            </Fragment>
        );
    }

}
